package com.fingerth.tinkerstudy

import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tencent.tinker.lib.tinker.Tinker
import com.tencent.tinker.lib.tinker.TinkerInstaller
import com.tencent.tinker.loader.shareutil.ShareConstants
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals
import java.io.File

class MainActivity : AppCompatActivity() {
    //    private val path = Environment.getExternalStorageDirectory().absolutePath + "/patch_signed_7zip.apk"
    private var path: String = ""



    private lateinit var textView: TextView
    private lateinit var iv: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getExternalFilesDir(Environment.DIRECTORY_PODCASTS)?.let { path = it.absolutePath + "/patch_signed_7zip.apk" }

        iv = findViewById(R.id.iv)
        textView = findViewById(R.id.tv_text)

        textView.text = if (path.isNotEmpty()) path else "补丁信息"


        iv.setImageURI(Uri.fromFile(File(Environment.getExternalStorageDirectory().absolutePath + "/AFK_Share.jpg")))
    }

    /**
     * 加载热补丁插件
     */
    fun loadPatch(v: View?) {
        TinkerInstaller.onReceiveUpgradePatch(applicationContext, path)
    }

    /**
     * 查看补丁信息
     */
    fun showInfo(v: View?) {
        // add more Build Info
        val sb = StringBuilder()
        val tinker = Tinker.with(applicationContext)
        if (tinker.isTinkerLoaded) {
            sb.append(String.format("[补丁已加载] \n"))
            sb.append(java.lang.String.format("[基准包版本号] %s \n", BuildConfig.TINKER_ID))
            sb.append(
                    String.format(
                            "[补丁号] %s \n",
                            tinker.tinkerLoadResultIfPresent.getPackageConfigByName(ShareConstants.TINKER_ID)
                    )
            )
            sb.append(
                    String.format(
                            "[补丁版本] %s \n",
                            tinker.tinkerLoadResultIfPresent.getPackageConfigByName("patchVersion")
                    )
            )
            sb.append(String.format("[补丁占用空间] %s k \n", tinker.tinkerRomSpace))
        } else {
            sb.append(String.format("[补丁未加载] \n"))
            sb.append(java.lang.String.format("[基准包版本号] %s \n", BuildConfig.TINKER_ID))
            sb.append(
                    String.format(
                            "[TINKER_ID] %s \n",
                            ShareTinkerInternals.getManifestTinkerID(applicationContext)
                    )
            )
        }
        textView.text = sb
    }

    /**
     * 清除补包
     */
    fun cleanPatch(v: View?) {
        Tinker.with(applicationContext).cleanPatch()
    }

    fun toast(v: View?) {
        Toast.makeText(this, "666", Toast.LENGTH_SHORT).show()
    }

}